//
//  UIView+ZLCategory.m
//  ZLCategory
//
//  Created by zhaozl on 2022/4/30.
//

#import "UIView+ZLCategory.h"

@implementation UIView (ZLCategory)

- (CGFloat)zl_x {
    return self.frame.origin.x;
}

- (void)setZl_x:(CGFloat)zl_x {
    CGRect rect = self.frame;
    rect.origin.x = zl_x;
    self.frame = rect;
}

- (CGFloat)zl_y {
    return self.frame.origin.y;
}

- (void)setZl_y:(CGFloat)zl_y {
    CGRect rect = self.frame;
    rect.origin.y = zl_y;
    self.frame = rect;
}

- (CGFloat)zl_width {
    return self.frame.size.width;
}

- (void)setZl_width:(CGFloat)zl_width {
    CGRect rect = self.frame;
    rect.size.width = zl_width;
    self.frame = rect;
}

- (CGFloat)zl_height {
    return self.frame.size.height;
}

- (void)setZl_height:(CGFloat)zl_height {
    CGRect rect = self.frame;
    rect.size.height = zl_height;
    self.frame = rect;
}

- (CGFloat)zl_right {
    return self.frame.origin.x + self.frame.size.width;
}

- (void)setZl_right:(CGFloat)zl_right {
    self.frame = CGRectMake(zl_right - self.zl_width, self.zl_y, self.zl_width, self.zl_height);
}

- (CGFloat)zl_bottom {
    return self.frame.origin.y + self.frame.size.height;
}

- (void)setZl_bottom:(CGFloat)zl_bottom {
    self.frame = CGRectMake(self.zl_x, zl_bottom - self.zl_height, self.zl_width, self.zl_height);
}

- (CGFloat)zl_centerX {
    return self.center.x;
}

- (void)setZl_centerX:(CGFloat)zl_centerX {
    CGPoint center = self.center;
    center.x = zl_centerX;
    self.center = center;
}

- (CGFloat)zl_centerY {
    return self.center.y;
}

- (void)setZl_centerY:(CGFloat)zl_centerY {
    CGPoint center = self.center;
    center.y = zl_centerY;
    self.center = center;
}

- (CGFloat)zl_cornerRadius {
    return self.layer.cornerRadius;
}

- (void)setZl_cornerRadius:(CGFloat)zl_cornerRadius {
    self.layer.cornerRadius = zl_cornerRadius;
}

- (CGFloat)zl_borderWidth {
    return self.layer.borderWidth;
}

- (void)setZl_borderWidth:(CGFloat)zl_borderWidth {
    self.layer.borderWidth = zl_borderWidth;
}

- (UIColor *)zl_borderColor {
    return [UIColor colorWithCGColor:self.layer.borderColor];
}

- (void)setZl_borderColor:(UIColor *)zl_borderColor {
    self.layer.borderColor = zl_borderColor.CGColor;
}

- (UIImage *)zl_shootImage {
    CGSize size = CGSizeMake(self.zl_width, self.zl_height);
    UIGraphicsBeginImageContext(size);
    UIGraphicsBeginImageContextWithOptions(size, YES, [UIScreen mainScreen].scale);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return viewImage;
}

- (UIViewController *)zl_getViewController {
    for (UIView *next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponseder = [next nextResponder];
        if ([nextResponseder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponseder;
        }
    }
    return nil;
}

- (void)zl_addTarget:(id)target action:(SEL)action {
    self.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:target action:action];
    [self addGestureRecognizer:tap];
}

- (void)zl_defaultShadow {
    self.layer.shadowColor = [UIColor colorWithRed:208/255.0 green:208/255.0 blue:208/255.0 alpha:0.300].CGColor;
    self.layer.shadowOffset = CGSizeMake(0, 3);
    self.layer.shadowOpacity = 1;
    self.layer.shadowRadius = 7;
}

#pragma mark - 手势

- (void)zl_addTap:(id)target sel:(SEL)sel {
    self.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:target action:sel];
    [self addGestureRecognizer:tap];
}

- (void)zl_addLongTap:(id)target sel:(SEL)sel {
    self.userInteractionEnabled = YES;
    UILongPressGestureRecognizer *tap = [[UILongPressGestureRecognizer alloc] initWithTarget:target action:sel];
    [self addGestureRecognizer:tap];
}

- (void)zl_addPan:(id)target sel:(SEL)sel {
    self.userInteractionEnabled = YES;
   UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:target action:sel];
   [self addGestureRecognizer:panGesture];
}

#pragma mark - layer

- (void)zl_cornersRadius:(CGFloat)radius corners:(UIRectCorner)corners {
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                                   byRoundingCorners:corners
                                                         cornerRadii:CGSizeMake(radius, radius)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}

- (void)zl_gradientsColors:(NSArray <UIColor *> *)colors {
    NSMutableArray *cgColors = [NSMutableArray array];
    for (UIColor *color in colors) {
        [cgColors addObject:(id)color.CGColor];
    }
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = self.bounds;
    gradientLayer.colors = cgColors.copy;
    gradientLayer.locations = @[@0.0, @1.0];
    [self.layer insertSublayer:gradientLayer atIndex:0];
}

@end
